﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Data;

namespace ProjectX
{
    public partial class Allcate : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userNick"] != null)
                {
                    this.username.Text = Session["userNick"].ToString();
                    this.loginlink.Visible = false;
                    this.reglink.Visible = false;
                    this.logout.Visible = true;
                }
                BindCatelist();
            }
        }

        private void BindCatelist()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            string catestr = "SELECT * FROM CidCount WHERE  Cidlevel = 1 AND [Count] > 0 ORDER BY [Count] DESC";
            //string catestr = "SELECT * FROM CidCount ORDER BY [Count] DESC";
            this.CateList.DataSource = sdh.ExecuteDataTable(catestr);
            this.CateList.DataBind();
        }

        protected void CateList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                SystemLib slb = new SystemLib();
                string cid = drv.Row["Cid"].ToString();
                string level = drv.Row["Cidlevel"].ToString();
                //string cateid = slb.Base64Encoding(cid);
                //string catelevel = slb.Base64Encoding(level);
                HyperLink hl = e.Item.FindControl("catelink") as HyperLink;
                hl.NavigateUrl = "Search/" + cid + "/" + level + ".html";
                //hl.Target = "_blank";
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                if (this.CateList.Items.Count == 0)
                {
                    this.CateList.ShowFooter = true;
                }
            }
        }

        protected void logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Write("<script language='javascript'>window.parent.location.href='/'</script>");
        }
    }
}